/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function selectAlbum(opt) {
    if (opt.value != '') {
        var albumId = opt.value;
        document.location = 'ShowMyAlbumServlet?albumId=' + albumId;
    }
}
function selectUsersAlbum(opt) {
    if (opt.value != '') {
        var userId = opt.value.split('@')[0];
        var albumId = opt.value.split('@')[1];
        document.location = 'ShowUserAlbumServlet?userId=' + userId + '&albumId=' + albumId;
    }
}
function resetQuery() {
    $('queryResult').innerHTML = '';
    $('queryResult').style.display = 'none';
    $('queryAlbum').value = '';
    $('queryUser').value = '';
    $('queryAlbum').focus();
    
}
function submitQuery(albumName, userName) {
    $('queryAlbum').focus();
    showLoading();
    new Ajax.Request('QueryUserAlbumServlet?albumName='+albumName+'&userName='+userName,
    {
        method: 'get',
        requestHeaders: {
            Accept: 'application/xml'
        },
        asynchronous: false,
        onSuccess: function (transport) {
            //alert(transport.responseText);
            var xml = transport.responseXML;
            if (xml != null && xml != '') {
                var result = xml.getElementsByTagName('status').item(0).firstChild.data;
                if (result == 'success') {
                    var html = '';
                    var itens = xml.getElementsByTagName('item');
                    if (itens.length > 0) {
                        html += '<span>' + queryTableTitle + '</span>'
                        html += '<table class="table-query" width="720">';
                        html += '<tr>';
                        html += '<td><b>' + queryTableAlbum + '</b></td>';
                        html += '<td><b>' + queryTableUser + '</b></td>';
                        html += '<td align="center"><b>' + queryTableTotal + '</b></td>';
                        html += '<td align="center"><b>' + queryTableOwned + '</b></td>';
                        html += '<td align="center"><b>' + queryTableMissing + '</b></td>';
                        html += '<td align="center"><b>' + queryTablePercentage + '</b></td>';
                        html += '<td></td>';
                        html += '</tr>';
                        for (var i = 0; i < itens.length; i++) {
                            var queryAlbumId = itens[i].getElementsByTagName('albumid').item(0).firstChild.data;
                            var queryAlbumName = itens[i].getElementsByTagName('albumname').item(0).firstChild.data;
                            var queryUserId = itens[i].getElementsByTagName('userid').item(0).firstChild.data;
                            var queryUserName = itens[i].getElementsByTagName('username').item(0).firstChild.data;
                            var queryTotalAlbumFigure = itens[i].getElementsByTagName('totalalbumfigure').item(0).firstChild.data;
                            var queryFigureOwned = itens[i].getElementsByTagName('figureowned').item(0).firstChild.data;
                            var queryFigureMissing = Number(queryTotalAlbumFigure) - Number(queryFigureOwned);
                            var tdClass = '';
                            if (i % 2 == 0) {
                                tdClass = 'odd';
                            }
                            else {
                                tdClass = 'even';
                            }
                            var linkStart = '<a href="ShowUserAlbumServlet?userId=' + queryUserId + '&albumId=' + queryAlbumId + '">';
                            var linkEnd = '</a>';
                            html += '<tr>';
                            html += '<td class=' +  tdClass + '>' + linkStart + queryAlbumName + linkEnd + '</td>';
                            html += '<td class=' +  tdClass + '>' + linkStart + queryUserName + linkEnd + '</td>';
                            html += '<td class=' +  tdClass + ' align="center">' + linkStart + queryTotalAlbumFigure + linkEnd + '</td>';
                            html += '<td class=' +  tdClass + ' align="center">' + linkStart + queryFigureOwned + linkEnd + '</td>';
                            html += '<td class=' +  tdClass + ' align="center">' + linkStart + queryFigureMissing + linkEnd + '</td>';
                            html += '<td class=' +  tdClass + ' align="center">' + linkStart + ((Number(queryFigureOwned) * 100) / Number(queryTotalAlbumFigure)).toFixed(0) + '%' + linkEnd + '</td>';
                            html += '<td align="right" class=' +  tdClass + '>' + linkStart + '<img src="images/report_user.png" border="0" />' + linkEnd + '</td>';
                            html += '</tr>';
                        }
                        html += '</table>';
                        $('queryResult').innerHTML = html;
                        $('queryResult').style.display = 'block';
                    }
                    closeLoading();
                }
                else {
                    alert('No data found');
                    $('queryResult').innerHTML = '';
                    $('queryResult').style.display = 'none';
                    closeLoading();
                }
            }
            else {
                alert('Error');
                $('queryResult').innerHTML = '';
                $('queryResult').style.display = 'none';
                closeLoading();
            }
            closeLoading();
        },
        onComplete: function () {
            closeLoading();
        },
        onFailure: function () {
            alert('Error');
            $('queryResult').innerHTML = '';
            $('queryResult').style.display = 'none';
            closeLoading();
        }
    });
}

function showUserRepeatedFigureSuggest(userId, userName, albumId, albumName) {
    if ($('div_' + userId + '_' + albumId).style.display == 'block') {
        $('div_' + userId + '_' + albumId).innerHTML = '';
        $('div_' + userId + '_' + albumId).style.display = 'none';
        return;
    }
    showLoading();
    new Ajax.Request('QueryUserRepeatedFigureSuggestedServlet?albumId='+albumId+'&userId='+userId,
    {
        method: 'get',
        requestHeaders: {
            Accept: 'application/xml'
        },
        asynchronous: false,
        onSuccess: function (transport) {
            //alert(transport.responseText);
            var xml = transport.responseXML;
            if (xml != null && xml != '') {
                var result = xml.getElementsByTagName('status').item(0).firstChild.data;
                if (result == 'success') {
                    var html = '';

                    var repeatedFigure = '';
                    if (xml.getElementsByTagName('repeatedfigure') != null && xml.getElementsByTagName('repeatedfigure').item(0).firstChild != null) {
                        repeatedFigure = xml.getElementsByTagName('repeatedfigure').item(0).firstChild.data;
                    }
                    var exchangeFigure = '';
                    if (xml.getElementsByTagName('exchangefigure') != null && xml.getElementsByTagName('exchangefigure').item(0).firstChild != null) {
                        exchangeFigure = xml.getElementsByTagName('exchangefigure').item(0).firstChild.data;
                    }
                    if (repeatedFigure == '') {
                        repeatedFigure = defaultNone;
                    }
                    if (exchangeFigure == '') {
                        exchangeFigure = defaultNone;
                    }
                    html += '<table width="100%">';
                        html += '<tr>';
                            html += '<td width="120">' + suggestionYouNeed + ': </td>';
                            html += '<td><font color="#0000ff"><b>' + repeatedFigure + '</b></font></td>';
                        html += '</tr>';
                        html += '<tr>';
                            html += '<td>' + suggestionYouExchange + ': </td>';
                            html += '<td><font color="#ff0000"><b>' + exchangeFigure + '</b></font></td>';
                        html += '</tr>';
                    html += '</table>';
                    $('div_' + userId + '_' + albumId).style.display = 'block';
                    $('div_' + userId + '_' + albumId).innerHTML = html;
                    closeLoading();
                }
                else {
                    closeLoading();
                }
            }
            else {
                closeLoading();
            }
            closeLoading();
        },
        onComplete: function () {
            closeLoading();
        },
        onFailure: function () {
            closeLoading();
        }
    });
}